using System;
using Avanzis.QueryFramework;
using NUnit.Framework;

namespace Avanzis.QueryBuilder.Nunit
{
    [TestFixture]
    public class SearchFiltersCollectionTest
    {
        [TestFixtureSetUp]
        public void Setup()
        {
            QueryFrameworkConfiguration.Dialect = Dialects.MSSql2005Dialect;
            QueryBuilder.QueryProcessor.ResetProcessor();
        }

        /// <summary>
        /// Searches the filters collection contructor.
        /// </summary>
        [Test]
        public void SearchFiltersCollectionContructor()
        {
            SearchFiltersCollection filters = new SearchFiltersCollection("Active = 1");
            Assert.AreEqual(1, filters.Count);
            Assert.AreEqual("(Active = 1)", filters.ToString());

            filters = new SearchFiltersCollection("Active = 1 AND Year = 2003");
            Assert.AreEqual(1, filters.Count);
            Assert.AreEqual("(Active = 1 AND Year = 2003)", filters.ToString());
        }

        [Test]
        public void SearchFiltersWitNullValues()
        {
            SearchFiltersCollection filters = new SearchFiltersCollection();

            filters.Add("FirstName", SqlOperator.Equals, null);            

            Assert.AreEqual("(FirstName IS NULL)", filters.ToString());
        }

        [Test]
        public void SearchFiltersWithComplexOperators()
        {
            SearchFiltersCollection filters = new SearchFiltersCollection();
            filters.Add("Subclass", SqlOperator.In, "('N','R','T')");
            Assert.AreEqual("(Subclass IN ('N','R','T'))", filters.ToString());
        }

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void CheckInvalidExpressionForIsOperator()
        {
            SearchFiltersCollection filters = new SearchFiltersCollection();

            filters.Add("FirstName", SqlOperator.Is, "John");

            filters.ToString();
        }

    }
}
